package com.qishao.service.impl;

import com.qishao.model.EduCourse;
import com.qishao.mapper.EduCourseMapper;
import com.qishao.model.EduCourseDescription;
import com.qishao.service.EduCourseDescriptionService;
import com.qishao.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishao.service_base.error.CustomException;
import com.qishao.vo.EduCourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author xushaoqi
 * @since 2022-01-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    private EduCourseDescriptionService courseDescriptionService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public EduCourse saveCourseInfo(EduCourseVo courseVo) {
        EduCourse course = new EduCourse();
        //使用beanutil 将两个不同的实体互转
        BeanUtils.copyProperties(courseVo,course);
        //增加课程信息
        int insert = baseMapper.insert(course);
        if(0 == insert){
            throw new CustomException("20001","增加课程信息错误");
        }
        //增加课程简介信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(courseVo.getDescription());
        courseDescriptionService.save(courseDescription);

        return course;
    }
}
